package org.demo01.realms;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class MyRealm extends AuthorizingRealm {

	/**
	 * 授权
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		System.out.println("用户授权！");
		//取出认证成功后的主身份信息
		String principal = (String) principals.getPrimaryPrincipal();
		//拿主身份去数据库查询所有的权限信息
		List<String> list = new ArrayList<>();
		list.add("user:*");
		list.add("role:query");
		
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		info.addStringPermissions(list);
		
		return info;
	}

	/**
	 * 用于身份认证
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		//取出身份信息
		String principal = (String) token.getPrincipal();
		//调用dao方法，根据身份从数据库查询用户信息
		if (!"aptech".equals(principal)) {
			return null;
		}
		return new SimpleAuthenticationInfo(principal, "e6b7bc4b60f2a303f28d0be3463de68d", ByteSource.Util.bytes("qwert"),this.getName());
	}

}






